home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / users.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  2KB  |  112 lines

  1. /*  users - list the users        Author: Terrence W. Holm */
  2.  
  3. /*  Usage: users
  4.  *
  5.  *  A simple users(1) command for MINIX. Assumes tty0
  6.  *  to tty9 are the only possible login devices.
  7.  *  See last.c for more robust code for reading wtmp.
  8.  */
  9.  
  10.  
  11. #include <sys/types.h>
  12. #include <utmp.h>
  13. #include <stdio.h>
  14.  
  15. #ifndef  WTMP
  16. #define  WTMP   "/usr/adm/wtmp"
  17. #endif
  18.  
  19. #define  min( a, b )     ( (a < b) ? a : b )
  20.  
  21. #define  BUFFER_SIZE     1024    /* Room for wtmp records  */
  22. #define  MAX_WTMP_COUNT  ( BUFFER_SIZE / sizeof(struct utmp) )
  23.  
  24. struct utmp wtmp_buffer[MAX_WTMP_COUNT];
  25.  
  26.  
  27. main()
  28. {
  29.   FILE *f;
  30.   long size;            /* Number of wtmp records in the file     */
  31.   int wtmp_count;        /* How many to read into wtmp_buffer     */
  32.   int used = 0;
  33.   int user_count = 0;
  34.   char users[10][8];
  35.  
  36.  
  37.   if ((f = fopen(WTMP, "r")) == NULL)    /* No login/logout records kept  */
  38.     exit(0);
  39.  
  40.   if (fseek(f, 0L, 2) != 0 || (size = ftell(f)) % sizeof(struct utmp) != 0) {
  41.     fprintf(stderr, "users: invalid wtmp file\n");
  42.     exit(1);
  43.   }
  44.   size /= sizeof(struct utmp);    /* Number of records in wtmp     */
  45.  
  46.  
  47.   while (size > 0) {
  48.     wtmp_count = (int) min(size, MAX_WTMP_COUNT);
  49.     size -= (long) wtmp_count;
  50.     fseek(f, size * sizeof(struct utmp), 0);
  51.     if (fread(&wtmp_buffer[0], sizeof(struct utmp), wtmp_count, f) !=
  52.         wtmp_count) {
  53.         fprintf(stderr, "users: read error on wtmp file\n");
  54.         exit(1);
  55.     }
  56.     while (--wtmp_count >= 0) {
  57.         int tty;
  58.  
  59.         if (strcmp(wtmp_buffer[wtmp_count].ut_line, "~") == 0) {
  60.             Print_Users(user_count, users);
  61.             exit(0);
  62.         }
  63.         tty = wtmp_buffer[wtmp_count].ut_line[3] - '0';
  64.  
  65.         if (tty < 0 || tty > 9) {
  66.             fprintf(stderr, "users: encountered unknown tty in wtmp file\n");
  67.             exit(1);
  68.         }
  69.         if (!(used & (1 << tty))) {
  70.             used |= 1 << tty;
  71.             memcpy(users[user_count], wtmp_buffer[wtmp_count].ut_name, 8);
  72.  
  73.             if (users[user_count][0] != '\0') ++user_count;
  74.         }
  75.     }
  76.  
  77.   }                /* end while( size > 0 ) */
  78.  
  79.   Print_Users(user_count, users);
  80.  
  81.   exit(0);
  82. }
  83.  
  84.  
  85.  
  86. Strncmp(str1, str2)
  87. char *str1;
  88. char *str2;
  89.  
  90. {
  91.   return(strncmp(str1, str2, 8));
  92. }
  93.  
  94.  
  95.  
  96. Print_Users(user_count, users)
  97. int user_count;
  98. char *users;
  99.  
  100. {
  101.   int i;
  102.  
  103.   qsort(users, user_count, 8, Strncmp);
  104.  
  105.   for (i = 0; i < user_count - 1; ++i) {
  106.     printf("%.8s ", users);
  107.     users += 8;
  108.   }
  109.  
  110.   if (user_count > 0) printf("%.8s\n", users);
  111. }
  112.